package com.sky.service.impl;

import com.baomidou.mybatisplus.extension.api.R;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.sky.entity.futu.*;
import com.sky.utils.UrlBuilder;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class FutuFundPositionService {

    private final OkHttpClient okHttpClient;
    private final Gson gson = new Gson();
    @Value("${futu.base-url}")
    private String BASE_URL;
    private final String prefix = "/fundPosition";

    // 拼接 BASE_URL 和 prefix
    private String getBaseUrlWithPrefix() {
        return BASE_URL + prefix;
    }
    @Autowired
    public FutuFundPositionService(OkHttpClient okHttpClient) {
        this.okHttpClient = okHttpClient;
    }

    private Object executeRequest(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .build();

        try (Response response = okHttpClient.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }
            // 解析响应体为Map，具体实现可根据需要调整
            return parseResponse(response.body().string()).getData();
        }
    }

    private FutuResponse parseResponse(String responseBody) {
        return gson.fromJson(responseBody, FutuResponse.class); // 替换为实际解析
    }

    public List<FundPositionInfo> getInfo() throws IOException {
        String url = getBaseUrlWithPrefix() + "/info";
        Object jsonResponse = executeRequest(url); // 假设executeRequest返回的是JSON字符串
        log.info("jsonResponse:{}", jsonResponse);
        return gson.fromJson(gson.toJson(jsonResponse), new TypeToken<List<FundPositionInfo>>() {
        }.getType());
    }

    public List<PositionInfo> getPositionList(String code, String plRatioMin, String plRatioMax) throws IOException {
        String url = new UrlBuilder(getBaseUrlWithPrefix() + "/positions")
                .addParam("code", code)
                .addParam("pl_ratio_min", plRatioMin)
                .addParam("pl_ratio_max", plRatioMax)
                .build();

        Object jsonResponse = executeRequest(url);
        log.info("jsonResponse:{}", jsonResponse);
        return gson.fromJson(gson.toJson(jsonResponse), new TypeToken<List<PositionInfo>>() {
        }.getType());
    }

    public List<AccTradingInfo> getAccTradingInfo(String orderType, String code, String price, String orderId) throws IOException {
        if (price == null || price.isEmpty() || code == null || code.isEmpty()) {
            return new ArrayList<>();
        }
        String url = new UrlBuilder(getBaseUrlWithPrefix() + "/get_acctradinginfo")
                .addParam("order_type", orderType)
                .addParam("code", code)
                .addParam("price", price) // price 是必填的
                .addParam("order_id", orderId)
                .build();

        Object jsonResponse = executeRequest(url);
        log.info("jsonResponse:{}", jsonResponse);
        return gson.fromJson(gson.toJson(jsonResponse), new TypeToken<List<AccTradingInfo>>() {
        }.getType());
    }

}
